/**
 * 
 * gislin
 * 2016年1月16日
 */
package com.szholly.pro.watergas_server.ps.schedule;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 统计日消减量
 * @author gislin
 * 2016年1月16日
 */
public class CalcAmount implements Job {
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		execute();
	}
	
	public void execute(){
		String sql = "select t1.*,amount_cou/in_cou amount_pre from (\n" +
				"    select sum(in_avg) - sum(out_avg) amount_avg,sum(in_avg) in_avg,sum(out_avg) out_avg,\n" + 
				"           sum(in_cou) - sum(out_cou) amount_cou,sum(in_cou) in_cou,sum(out_cou) out_cou,wwtp_id,parmname,datatime from (\n" + 
				"      select mdd.cou in_cou,0 out_cou,ms.wwtp_id,mdd.parmname,mdd.datatime,\n" + 
				"             mdd.avg in_avg,0 out_avg\n" + 
				"             from BC_COLLECTION_INFO dc\n" + 
				"             left join BC_SITE_INFO ms on dc.site_id = ms.exp_no\n" + 
				"             left join monitor_data_day mdd on mdd.mn = dc.device_num\n" + 
				"             where ms.wwtp_id is not null and ms.flow_type = '0' and mdd.cou > 0\n" + 
				"      union all\n" + 
				"      select 0 in_cou,mdd.cou out_cou,ms.wwtp_id,mdd.parmname,mdd.datatime,\n" +
				"             0 in_avg,mdd.avg out_avg\n" + 
				"             from BC_COLLECTION_INFO dc\n" + 
				"             left join BC_SITE_INFO ms on dc.site_id = ms.exp_no\n" + 
				"             left join monitor_data_day mdd on mdd.mn = dc.device_num\n" + 
				"             where ms.wwtp_id is not null and ms.flow_type = '1'\n" + 
				"           ) t group by t.wwtp_id,t.parmname,t.datatime\n" + 
				"         ) t1 where t1.in_cou > 0 and t1.out_cou > 0 and t1.datatime >= @datatime ";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("datatime", getLastCalcDate()));
		
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		
		String deleteSql = " DELETE FROM MONITOR_DATA_DAY_AMOUNT WHERE DATATIME >= @datatime ";
		DataUtil.ExecuteUpdateSql(deleteSql, parms);
		
		for(RowBase row : rows){
			String wwtp_id = row.getSafeString("WWTP_ID");
			String in_cou = row.getSafeString("IN_COU");
			String out_cou = row.getSafeString("OUT_COU");
			String in_dens = row.getSafeString("IN_AVG");
			String out_dens = row.getSafeString("OUT_AVG");
			String amount_out = row.getSafeString("AMOUNT_COU");
			String amount_pre = row.getSafeString("AMOUNT_PRE");
			String amount_dens = row.getSafeString("AMOUNT_AVG");
			String pollname = row.getSafeString("PARMNAME");
			Date datatime = row.getDate("DATATIME");
			
			String insertSql = " INSERT INTO MONITOR_DATA_DAY_AMOUNT(GUID,WWTP_ID,IN_COU,OUT_COU,IN_DENS,OUT_DENS,AMOUNT_COU,AMOUNT_PRE,AMOUNT_DENS,DATATIME,POLLNAME)"
					+ " VALUES(sys_guid(),@WWTP_ID,@IN_COU,@OUT_COU,@IN_DENS,@OUT_DENS,@AMOUNT_COU,@AMOUNT_PRE,@AMOUNT_DENS,@DATATIME,@POLLNAME) ";
			
			parms.clear();
			parms.add(new GlobalParameter("WWTP_ID", wwtp_id));
			parms.add(new GlobalParameter("IN_COU", in_cou));
			parms.add(new GlobalParameter("OUT_COU", out_cou));
			parms.add(new GlobalParameter("IN_DENS", in_dens));
			parms.add(new GlobalParameter("OUT_DENS", out_dens));
			parms.add(new GlobalParameter("AMOUNT_COU", amount_out));
			parms.add(new GlobalParameter("AMOUNT_PRE", amount_pre));
			parms.add(new GlobalParameter("AMOUNT_DENS", amount_dens));
			parms.add(new GlobalParameter("POLLNAME", pollname));
			parms.add(new GlobalParameter("DATATIME", datatime));
			
			DataUtil.ExecuteUpdateSql(insertSql, parms);			
		}
	}
	
	/**
	 * 获取最后一次计算的日期
	 * gislin
	 * 2016年1月16日
	 * @return
	 */
	public Date getLastCalcDate(){
		Date lastCalcDate =  null;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -20);
		lastCalcDate = cal.getTime();
		return lastCalcDate;
	}
}
